$(function(){
	searchAuthByDeptId();
});

//全局变量,树1 , 树2
var Tree1, Tree2;
var datad=[];
var datad1=[];

//ztree 的设置.对于标准格式数据.不需要设置我注释的那部分.
//那部分属于简单格式数据需要设置的...不清楚去看ztree demo
var setting = {
	checkable: true
};

//数据右移动
function addRole() {
	var deptId = $("#department").val();
	if(1){
		var adminId = $("#adminId").val();
		//处理获取的值(权限Id,是否允许分部门)
		var addAuth = getAuthData(Tree1);
		alert(addAuth);
		if(addAuth==""){
			show_err_msg("请选择权限");
			return false;
		}else{
			var xmlhttp;
		    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		        xmlhttp = new XMLHttpRequest();
		    }else {// code for IE6, IE5
		        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		    }
		    xmlhttp.onreadystatechange = function () {
		        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		            var obj;
		            try{
		                obj =  eval('(' + xmlhttp.responseText + ')');
		            }catch(exception){
		                obj="error";
		            }
		            if(obj == "error"){
		                show_err_msg("服务链接出错！");    
		            }else{
		                if(obj.status == "succeed"){
		                	show_msg(obj.info,'');
		                	searchAuthByDeptId();
		                }else if(obj.status == "fail"){
							show_err_msg(obj.info);
		                }
		            }
		        }
	        }
		    xmlhttp.open("POST", "/ajaxinsert/distributAuthRight/", true);
		    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		    xmlhttp.send("adminId="+adminId+"&deptId="+deptId+"&nodes="+addAuth);
		}
	}else{
        show_err_msg("您目前没有给用户分配权限的权限,请联系管理员");
    }
}

//数据左移动
function delRole() {
	var deptId = $("#department").val();
	if(ajaxFun("/ajax/checkAuthority/?functionId=web_user_setPermit&deptId=")){
		var adminId = $("#adminId").val();
		//处理获取的值(权限Id,是否允许分部门)
		var addAuth = getAuthData(Tree2);
		if(addAuth==""){
			show_err_msg("请选择权限");
			return false;
		}else{
			var xmlhttp;
		    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		        xmlhttp = new XMLHttpRequest();
		    }else {// code for IE6, IE5
		        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		    }
		    xmlhttp.onreadystatechange = function () {
		        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		            var obj;
		            try{
		                obj =  eval('(' + xmlhttp.responseText + ')');
		            }catch(exception){
		                obj="error";
		            }
		            if(obj == "error"){
		                show_err_msg("服务链接出错！");    
		            }else{
		                if(obj.status == "succeed"){
		                	show_msg(obj.info,'');
		                	searchAuthByDeptId();
		                }else if(obj.status == "fail"){
							show_err_msg(obj.info);
		                }
		            }
		        }
	        }
		    xmlhttp.open("POST", "/ajaxinsert/distributAuthLeft/", true);
		    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		    xmlhttp.send("adminId="+adminId+"&deptId="+deptId+"&nodes="+addAuth);
		}
	}else{
        show_err_msg("您目前没有给用户分配权限的权限,请联系管理员");
    }
}
function searchAuthByDeptId(){
	var deptId = $("#department").val();
	var adminId = $("#adminId").val();
	datad=[];
	datad1=[];
	var special=$("#specialFunction");
	var refush=$("#refushFunction");
	refush.html("");
	special.html("");
	if(deptId == null ||deptId ==""){
		initTree(datad,datad1);
		return false;
	}else{
		var xmlhttp;
	    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
	        xmlhttp = new XMLHttpRequest();
	    }else {// code for IE6, IE5
	        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	    }
	    xmlhttp.onreadystatechange = function () {
	        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
	            var obj;
	            try{
	                obj =  eval('(' + xmlhttp.responseText + ')');
	            }catch(exception){
	                obj="error";
	            }
	            if(obj == "error"){
	                //show_err_msg("服务链接出错！");
	                var data = [{},{'code':12,'id':'ss','name':'ddd','byDept':1,'subSystem':'cvb'},{},{},{},{},{},{},{},{},{},{},{},{},{},{}];    
	                initTree(data);
	            }else{
	                if(obj.status == "succeed"){
	                	datad=obj.out==""?[]:eval('(' + obj.out+ ')');
	                	datad1=obj.own==""?[]:eval('(' + obj.own+ ')');
	                	special.html(obj.ownSpecial);
	                	refush.html(obj.ownRefush);
	                	initTree(datad,datad1);
	                }else if(obj.status == "fail"){
						show_err_msg(obj.info);
	                }
	            }
	        }
        }
	    xmlhttp.open("POST", "/ajax/auth/", true);
	    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	    xmlhttp.send("deptId="+deptId+"&adminId="+adminId);
	}
}
function getAuthData(zTree1){
	var nodes = zTree1.getCheckedNodes();
	var strs = "";

	for(var i=1;i<nodes.length;i++){
		var node = nodes[i];
		if(node.code=="parent")
			continue;
	    strs+='{"functionId":"'+node.id+'","byDept":"'+node.byDept+'","subSystem":"'+node.subSystem+'"}*';
	}
	return strs;
}
function initTree(datad,datad1){
	//树1 数据 生成
	Tree1 = $("#srcTree").zTree(setting, datad);	
	//树2 数据null
	Tree2 = $("#tarTree").zTree(setting, datad1);
}
function deleteSpecial(){
	var deptId = $("#department").val();
	if(ajaxFun("/ajax/checkAuthority/?functionId=web_user_setPermit&deptId=")){
		var adminId = $("#adminId").val();
		var functions="";
		$(".special").each(function(){
			var divId = $(this).attr("id");
			if(document.getElementById(divId).checked)
				functions+=$(this).attr("value")+",";
		});
		if(functions == null ||functions ==""){
			show_err_msg("请选择要删除的权限！");   
			return false;
		}else{
			var xmlhttp;
		    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		        xmlhttp = new XMLHttpRequest();
		    }else {// code for IE6, IE5
		        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		    }
		    xmlhttp.onreadystatechange = function () {
		        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		            var obj;
		            try{
		                obj =  eval('(' + xmlhttp.responseText + ')');
		            }catch(exception){
		                obj="error";
		            }
		            if(obj == "error"){
		                show_err_msg("服务链接出错！");    
		            }else{
		                if(obj.status == "succeed"){
		                	show_msg(obj.info,'');
		                	searchAuthByDeptId();
		                }else if(obj.status == "fail"){
							show_err_msg(obj.info);
		                }
		            }
		        }
		    }
		    xmlhttp.open("POST", "/ajaxDelete/tableSpecialDelete/", true);
		    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		    xmlhttp.send("deptId="+deptId+"&adminId="+adminId+"&permit="+1+"&functions="+functions);
		}
	}else{
        show_err_msg("您目前没有给用户移除权限的权限,请联系管理员");
    }
}
function deleteRefush(){
	var deptId = $("#department").val();
	if(ajaxFun("/ajax/checkAuthority/?functionId=web_user_setPermit&deptId=")){
		var adminId = $("#adminId").val();
		var functions="";
		$(".refush").each(function(){
			var divId = $(this).attr("id");
			if(document.getElementById(divId).checked)
				functions+=$(this).attr("value")+",";

		});
		if(functions == null ||functions ==""){
			show_err_msg("请选择要删除的权限！");   
			return false;
		}else{
			var xmlhttp;
		    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		        xmlhttp = new XMLHttpRequest();
		    }else {// code for IE6, IE5
		        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		    }
		    xmlhttp.onreadystatechange = function () {
		        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		            var obj;
		            try{
		                obj =  eval('(' + xmlhttp.responseText + ')');
		            }catch(exception){
		                obj="error";
		            }
		            if(obj == "error"){
		                show_err_msg("服务链接出错！");    
		            }else{
		                if(obj.status == "succeed"){
		                	show_msg(obj.info,'');
		                	searchAuthByDeptId();
		                }else if(obj.status == "fail"){
							show_err_msg(obj.info);
		                }
		            }
		        }
		    }
		    xmlhttp.open("POST", "/ajaxDelete/tableSpecialDelete/", true);
		    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		    xmlhttp.send("deptId="+deptId+"&adminId="+adminId+"&permit="+0+"&functions="+functions);
		}
	}else{
        show_err_msg("您目前没有给用户移除权限的权限,请联系管理员");
    }
}